package com.jiazhong.mingxing.boot.boot11.普通消息.消息消费;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

@Slf4j
public class 探讨消费问题 {
    /*@SneakyThrows
    public static void main(String[] args) {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("jiazhong-consumer-a");
        consumer.setNamesrvAddr("124.220.170.254:9876");
        consumer.subscribe("TopicA", "sync");
        consumer.registerMessageListener((MessageListenerConcurrently) (list,
                                                                        consumeConcurrentlyContext) -> {
            log.info("有订阅的消息生产后，这里会自动执行");

            int a = (int) (Math.random() * 100 + 1);
            log.info("a:{}", a);
            if (a % 3 == 0) {
                // 失败消费了 需要过会重试
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
            // 成功消费了
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        consumer.start();
    }*/

    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("jiazhong-consumer-a");
        consumer.setNamesrvAddr("124.220.170.254:9876");
        consumer.subscribe("TopicA", "sync");
        consumer.registerMessageListener((MessageListenerConcurrently) (list,
                                                                        consumeConcurrentlyContext) -> {
            log.info("有订阅的消息生产后，这里会自动执行");

            log.info("list的size:{}", list.size());
            // 错误
            // log.info("0:{},1:{}",list.get(0),list.get(1));
            // 成功消费了
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        consumer.start();
    }
}
